<?php

	class Database
	{
		public $link;
		
		public $log_sql = false;
		
		function __construct( &$link )
		{
			$this->link =& $link;
		}
		
		public static function connect( $host, $username, $password, $table )
		{
			error_log( "connecting to mysql://$username:$password@$host/$table" );
			
			$link = mysql_connect( $host, $username, $password );
			
			if ( $link )
				mysql_select_db( $table, $link );
			
			if ( $error = mysql_error() )
			{
				error_log( "MySQL connect problem" );
				error_log( $error );
				return false;
			}
			
			return new Database( $link );
		}
		
		public function query( $query )
		{
			if ( ! $this->link )
				return false; // database connection lost
			
			if ( ! $result = mysql_query( $query ) )
				return $this->log( mysql_error( $this->link ) );
			
			if ( $result && $result !== true )
				while( $row = mysql_fetch_assoc( $result ) )
					$rows[] = $row;
			else
				return $result;
				
			return $rows;
		}
		
		public function log( $message )
		{
			if ( $this->log_sql )
				error_log( $message );
			
			return false;
		}
	}

?>